home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4876 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: freeing structures
  5. Date: 7 Feb 1996 17:35:01 GMT
  6. Organization: OpenVision
  7. Message-ID: <4fans5$htt@spanky.pls.ov.com>
  8. References: <4f7913$brg@sepia.nioz.nl>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article brg@sepia.nioz.nl, rikko@nioz.nl (Rikko Verrijzer) writes:
  13. >Hi,
  14. >
  15. >I have learnt C++ at my school past year, but now I have to build a programm in just
  16. >plain Standard C. the problem with this is, that several C types where new to me.
  17. >as example typedef.... in C++ I used a class to do this...
  18. >
  19. >/*
  20. > * Data structure used for storing the loaded file.
  21. > */
  22. >
  23. >typedef struct  Line {
  24. >
  25. >        struct  Line  *next;    /* Pointer to next item        */
  26. >        char    **dpart[3];
  27. >        char    **DateLine[3];  /* contains the date in form yy-mm-dd */
  28. >        char    *RestLine;      /* list of the Values of the line */
  29. >} Line;
  30. >
  31. >In my programm I have to load a ascii file, my approch was to build a linked list
  32. >of struct line (which I defiened with typedef Line), a struct line will contain data
  33. >and a pointer to the next line. this part went fine but the problems starts with
  34. >clearing a file from memory, my approch to that was as follows. The errors created
  35. >by this function occor in two different ways, 1) they damage a line which was loaded
  36. >after the clearing 2) causes the programm to crash with a segmentation fault.
  37. >I know I have made a mistake in my memeroy administration but, I can't find the problem
  38. >
  39. >void ClearLines()
  40. >{
  41. >Line*   line;
  42. >Line*   line_next;
  43. >        line=FirstLine;
  44. >        /*
  45. >         * when no file is loaded the firstline will be NULL
  46. >         */
  47. >        while(line!=(Line*)NULL && line->next!=(Line*)NULL)
  48. >        {
  49. >                /*
  50. >                 * ask for the next line while the currentline is still around
  51. >                 */
  52. >                line_next=line->next;
  53. [snipped a lot]
  54. >                free(&line);  /* I suspect this line to trigger the error */
  55.                       ^
  56. This is the problem.  You must never give anything to free() that was not
  57. generated by malloc().  The above statement should be:
  58.                  free(line);
  59.  
  60.             Fletcher.Glenn@ov.com
  61.  
  62. >                line=line_next;
  63. >        }
  64. >}
  65. >
  66. >
  67. >I hope some can find what I did wrong, I'm sure it is something simple but I can't 
  68. >find it....
  69. >
  70. >Thanx for reading my question
  71. >
  72. >please email me if you have a solution... (rikko@nioz.nl)
  73. >
  74. >    Greetings
  75. >        Rikko
  76. >
  77. >
  78.  
  79.  
  80.  
  81.  
  82.  
  83.